<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fm" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>医嘱记录</title>
</head>
<body>
<style type="text/css">
#getRecord .a-Css{
    display: inline-block;
    border: 1px solid gray;
    background: #dee;
    padding: 2px 14px;
    border-radius: 5px;
    text-decoration: none;
    color: black;
    cursor: pointer;
}
.ljw-span{
    display: inline-block;
    width: 100%;;
    height: 20px;
}
.dep-span{
	float: right;
	width: 100px;
	text-align: center;
}
.person-box-nurse{
	box-shadow: 0px 0px 2px 2px #ddd;
	margin: 10px 15px;
	border: 1px solid #A8A8A8;
	border-radius:5px;
	width:240px;
	display: inline-block;
	position:relative;
	cursor: pointer;
}  
.person-img{
	box-shadow: 0px 2px #ddd;
	border: 3px solid white;
	width: 60px;
	float:left;
}
.person-nameBox{
	float:left;
	width:140px;
	padding-left: 15px;
}
.nameSpan{
	font-weight: bold;
	font-size: 18px;
}
.timeBox{
    padding-left: 10px;
    padding-bottom: 4px;
    font-size: 13px;
}
.imgBox{
	overflow: auto;
	padding: 8px 8px 2px 8px;
}
.stateBox{
	background: #E0ECF4;
	text-align: center;
	line-height:30px;
}
.person-nurseWin .text-css{
	border: 0px;
    border-bottom: 1px solid #aaa;
    background: #dee;
    text-align: center;
    width: 80px;
    height: 24px;
    outline: none;
    font-style: italic;
}
.a-Css{
    display: inline-block;
    border: 1px solid gray;
    background: #dee;
    padding: 2px 10px;
    border-radius: 5px;
    text-decoration: none;
    color: black;
    cursor: pointer;
}
.a-color{
	color: #d84242;
}
</style>
<script type="text/javascript">
$(function(){
	//卡片悬浮事件
	$(".person-box-nurse").hover(
		function(){
			$(this).stop().css("box-shadow","0px 0px 4px 4px #ddd");
		},
		function(){
			$(this).stop().css("box-shadow","0px 0px 2px 2px #ddd");
		}
	);
	
	//患者卡片点击事件
	$(".person-box-nurse").click(function(){
		$(".person-nurseWin").window("open");
		$('#nuserTabs').tabs('select','医嘱执行');//默认选中第一个选项卡
		var hid = $(this).attr('id');
		$.post('ljw-patient-data',
			{"hid":hid},
			function(data){
				console.log(data);
				$("#record_Nurse input[name='patient_hid']").val(hid);
				$("#record_Nurse input[name='bedText']").val(data[0].bed_number);
				$("#record_Nurse input[name='nameText']").val(data[0].patient_name);
				$("#record_Nurse input[name='sexText']").val(data[0].patient_sex);
				$("#record_Nurse input[name='ageText']").val(data[0].patient_age);
				$("#record_Nurse input[name='balanceText']").val((data[0].balance).toFixed(2));
			}
		);
		//初始化表格数据
		initAdviceDetails(hid);
	});
	
	//选项卡点击时刷新表格数据
	$("#nuserTabs").tabs({
		onSelect:function(title){
			if(title=="执行记录"){
				var hid = $("input[name='patient_hid']").val();
				//执行记录的表格初始化数据
				$("#excuteTable").datagrid({
					width:'100%',
					fitColumns:true,
					url:"ljw-advice-record?hid="+hid,
					columns:[[
						{field:'Clearing_details_id',title:'执 行 编 号',width:100,halign:'center'},
						{field:'clearing_item',title:'执 行 名 称',width:160,halign:'center'},
						{field:'clearing_type',title:'类 型',width:80,halign:'center'},
						{field:'Clearing_details_price',title:'单 价',width:80,halign:'center',
							formatter:function(value,row,index){
								return "￥"+value.toFixed(2);
							}	
						},
						{field:'clearing_unit',title:'单 位',width:80,halign:'center'},
						{field:'Clearing_details_number',title:'数 量',width:80,halign:'center'},
						//{field:'invoice_state',title:'状 态',width:80,halign:'center'},
						{field:'tally_time',title:'执 行 时 间',width:150,halign:'center',
							formatter:function(value,row,index){
								var time = new Date(value).toJSON();
								var date = new Date(+new Date(time)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'') ;
								return date;
							}		
						},
						{field:'clearing_name',title:'执 行 护 士',width:100,halign:'center'},
					]],
				});
			}
		}
	}); 
	
	$('#deptBox').combobox({
	    url:'ljw-departments',//数据源
	    valueField:'departments_id',//实际值
	    textField:'departments_name',//显示值
	    readonly:true,
	    onLoadSuccess:function(rows){
	    	var id = ${user.departmentsPx.departmentsId };
	    	for (var i = 0; i < rows.length; i++) {
				var row = rows[i];
				if(row.departments_id == id){
					$('#deptBox').combobox('select', row.departments_id);
					//$('#deptBox').combobox('disable');
				}
			}
	   	},
	   	onChange:function(newValue,oldValue){
	   		$("#deTable").datagrid({
	   			url:"ljw-repertory?eid="+newValue,
	   		});
	   	}
	});     
 
	//药品信息表格
	$("#deTable").datagrid({
		pagination:true,
		pageSize:9,
	    pageList:[9,20],
		url:"ljw-repertory?eid="+$('#deptBox').combobox('getValue'),
		columns:[[ 
			{field:'drug_id',hidden:true},
			{field:'d',checkbox:true},
			{field:'drug_name',title:'药 品 名 称',width:200,halign:'center'},
			{field:'drug_types_name',title:'类 型',width:100,align:'center'},
			{field:'drug_selling_price',title:'单 价',width:90,halign:'center',
				formatter:function(value,row,index){
					return "￥" + value.toFixed(2);
				}	
			},
			{field:'drug_specifications',title:'单 位',width:50,halign:'center'},
			{field:'amount',title:'库 存',width:60,halign:'center'}
		]]
	}); 
	
	//请领表格
	$("#getTable").datagrid({
		columns:[[ 
			{field:'drug_id',hidden:true},
			{field:'drug_name',title:'药 品 名 称',width:200,halign:'center'},
			{field:'drug_types_name',title:'类 型',width:90,align:'center'},
			{field:'drug_specifications',title:'单 位',width:70,halign:'center'},
			{field:'amount',title:'请 领 数',width:80,halign:'center',
				editor:{
			       type:'numberspinner',
			       options:{min:1,required: true}
			    }	
			}
		]],
		onClickRow:function(index){//设置不可选中
			$(this).datagrid('unselectRow',index);
		},
		onDblClickCell: function(index,field,value){
			$(this).datagrid('beginEdit', index);
			var ed = $("#getTable").datagrid('getEditor', {index:index,field:"amount"});
			$(ed.target).focus();
		}
	}); 
	
	//添加按钮
	$("#appendDrug").click(function(){
		var rows = $("#deTable").datagrid("getSelections");
		var rows2 = $("#getTable").datagrid("getRows");
		if(rows.length == 0)return;
		var amount = 50;//默认请领数量
		if($("input[name='amount']:checked").val()==2){
			amount = 100;
		}
		$.each(rows,function(key,value){
			var opin = true;
			for (var i = 0; i < rows2.length; i++) {
				var r = rows2[i];
				if(r.drug_id == value.drug_id){
					opin = false;
					r.amount = r.amount + amount;
					$('#getTable').datagrid('refreshRow', i);
				}
			}
			if(opin){
				$('#getTable').datagrid('appendRow',{
					drug_id: value.drug_id,
					drug_name: value.drug_name,
					drug_types_name: value.drug_types_name,
					drug_specifications: value.drug_specifications,
					amount: amount,
				});
			}
		})
		$('#deTable').datagrid('clearSelections');
	});
	
	//清除按钮
	$("#clearBtn").click(function(){
		console.log("清除");
		$("#getTable").datagrid("loadData", { total: 0, rows: [] });
	});
	
	//请领按钮
	$("#getBtn").click(function(){
		var rows = $("#getTable").datagrid("getRows");
		if(rows.length==0)return false;
		$("<form method='post'>").appendTo("body").form({
			url:'ljw-invoice',
			onSubmit:function(param){
				//申请人
				param["invoice.invoiceName"] = "${user.userName }";
				//申请状态
				param["invoice.invoiceState"] = "待发药";
				//科室
				param["invoice.departmentsPx.departmentsId"] = $('#deptBox').combobox("getValue");
				var total = 0;//总数量
				for (var i = 0; i < rows.length; i++) {
					$("#getTable").datagrid('endEdit',i);//关闭编辑器
					var row = rows[i];
					//数量
					param["details["+i+"].invoiceDetailsNumber"]= parseInt(row.amount);
					//医嘱详情id
					param["details["+i+"].adviceDetail"]= row.Advice_Details_id;
					//药品id
					param["details["+i+"].drugsYk.drugId"]= row.drug_id;
					total = total + parseInt(row.amount);
				}
				//总数量
				param["invoice.invoiceNumber"] = total;
				$("#getTable").datagrid("loadData", { total: 0, rows: [] });//清空表格
			},
			success:function(data){
				if(data==1){
					$.messager.show({
						title:'提 示',
						msg:'请 领 成 功',
						height:200,
						timeout:800,
						showType:'slide'
					});
				}else{
					$.messager.show({
						title:'提 示',
						msg:'请 领 失 败',
						height:200,
						timeout:800,
						showType:'slide'
					});
				}
			}
		}).submit();
	});
	
	//选项卡点击时刷新表格数据
	$(".invoTabs").tabs({
		onSelect:function(title){
			if(title=="请领记录"){
				$('#invoiceRecordTable').datagrid('reload'); 
			}
		}
	}); 
	
	//请领记录表格
	$("#invoiceRecordTable").datagrid({
		pagination:true,
		pageSize:9,
	    pageList:[9,20],
		url:"ljw-invoice-record",
		columns:[[ 
			{field:'invoice_id',title:'编 号',width:60,halign:'center'},
			{field:'departments_name',title:'请 领 科 室',width:90,halign:'center'},
			{field:'invoice_time',title:'请 领 时 间',width:150,align:'center',
				formatter:function(value,row,index){
					var time = new Date(value).toJSON();
					var date = new Date(+new Date(time)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'') ;
					return date;
				}	
			},
			{field:'invoice_name',title:'请 领 人',width:70,halign:'center'},
			{field:'invoice_state',title:'状 态',width:70,halign:'center'},
			{field:'handle',title:'操 作',width:70,align:'center',
				formatter:function(value,row,index){
					return "<a onclick='details("+row.invoice_id+")' class='a-Css'>详 情</a>";
				}
			}
		]],
		onClickRow:function(index,row){//设置不可选中
			$(this).datagrid('unselectRow',index);
		}
	});
	
	//药品搜索框
	$("#deptDrugSear").searchbox({
		searcher:function(value){
			var eid = $('#deptBox').combobox('getValue');
			$("#deTable").datagrid({
				url:"ljw-repertory?dname="+value+"&eid="+eid
			});
		}
	});
	
	//科室搜索框
	$("#invoiceRecordSear").searchbox({
		searcher:function(value){
			$("#invoiceRecordTable").datagrid({
				url:"ljw-invoice-record?dname="+value,
			});
		}
	});

});

//请领详情
function details(value){
	$(".detailsWin").window("open");
	$("#detailsTable").datagrid({
		pagination:true,
		pageSize:8,
	    pageList:[8,20],
		url:"ljw-invoice-details?inid="+value,
		columns:[[ 
			{field:'drug_id',title:'药 品 编 号',width:80,halign:'center'},
			{field:'drug_name',title:'药 品 名 称',width:200,halign:'center'},
			{field:'drug_types_name',title:'类 型',width:90,align:'center'},
			{field:'drug_specifications',title:'单 位',width:70,halign:'center'},
			{field:'invoice_details_number',title:'请 领 数',width:80,halign:'center'}
		]],
		onClickRow:function(index,row){//设置不可选中
			$(this).datagrid('unselectRow',index);
		}
	});
}

//执行医嘱
function execute(aid,money){
	var bal = $("input[name='balanceText']").val();
	if((bal-money)<0){
		$("#nurseTip").tooltip("show");
		return;
	}
	$.post('ljw-ex-advice',{'aid':aid},
		function(data){
			if(data=='成功'){
				$("input[name='balanceText']").val((bal-money).toFixed(2))
				$("#oneTable").datagrid("reload");//刷新表格
			}else{
				$.messager.show({
					title:'提 示',
					msg: data,
					height:200,
					timeout:500,
					showType:'slide'
				});
			}
		}	
	);
}

$('#nurseTip').tooltip({//提示消息
    position: 'right',
    content: '<span style="color:red"> 押金余额不足! </span>',
    showEvent: null 
});

/* 医嘱详情表格初始化 */
function initAdviceDetails(hid){
	$("#oneTable").datagrid({
		width:'100%',
		url:'ljw-advice-details?hid='+hid+"&val=2",
	    columns:[[
			{field:'Advice_Details_id',hidden:true},
			{field:'invoice_details',hidden:true},
			{field:'advice_time',title:'开 嘱 时 间',width:260,halign:'center',
				formatter:function(value,row,index){
					var time = new Date(value).toJSON();
					var date = new Date(+new Date(time)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'') ;
					return date;
				}	
			},
			{field:'advice_days',title:'医 嘱 类 型',width:140,align:'center',
				formatter:function(value,row,index){
					if(value==1){
						return "临时";
					}
					if(value==2){
						return "长期";
					}
					return "错误";
				}		
			},
			{field:'drug_name',title:'医 嘱 名 称',width:300,halign:'center'},
			{field:'drug_types_name',title:'类 别',width:100,align:'center'},
			{field:'Advice_Details_number',title:'数 量',width:100,halign:'center'},
			{field:'drug_specifications',title:'单 位',width:100,halign:'center'},
			{field:'drug_selling_price',title:'单价',width:120,halign:'center',
				formatter:function(value,row,index){
					return "￥"+value.toFixed(2);
				}		
			},
			{field:'Advice_Details_hz',title:'频 率',width:150,halign:'center'},
			/* {field:'advice_entrust',title:'医 生 嘱 咐',width:250,halign:'center'}, */
			{field:'handle',title:'操 作',width:150,align:'center',
				formatter:function(value,row,index){
					//if(row.advice_days==2){
						return "<a onclick='execute("+row.Advice_Details_id+','+(row.Advice_Details_number*row.drug_selling_price)+")' class='a-Css a-color'>确认执行</a>";
					//}
				}	
			},
	    ]],
	    rowStyler: function (index, row) {//行样式
           	$("#oneTable").datagrid('unselectRow',index);
            return 'background-color:#3FBA93;color: white;';
        },
        onClickRow:function(index,row){//设置不可选中
			$(this).datagrid('unselectRow',index);
		}
	});
}

</script>
<div class="easyui-tabs invoTabs" style="width:100%;height:100%;">
	<div title="患者列表" >
		<!-- 患者卡片  Start -->
		<div id="patientBox" style="width:1120px;margin: 15px auto;">
		<c:forEach items="${array }" var="p">
			<div class="person-box-nurse" onclick="" id="${p.hospital_id }">
				<div class="imgBox">
					<c:if test="${p.patient_sex == '女'}">
					<img class="person-img" src="/static/img/女士.png">
					</c:if>
					<c:if test="${p.patient_sex == '男'}">
					<img class="person-img" src="/static/img/男士.png">
					</c:if>
					<div class="person-nameBox" >
						<span class="ljw-span nameSpan">${p.patient_name }</span>
						<p class="ljw-span">
							<span>${p.patient_age }</span>岁
							<span class="dep-span">${p.departments_name }</span>
						</p>
					</div>
				</div>
				<div class="timeBox">
					<span>
						<fm:formatDate value="${p.hospital_time }" pattern="yyyy-MM-dd  hh:mm"/>
					</span>入院
					<span>${p.days }天</span>
				</div>
				<div class="stateBox">
					${p.hospital_state }
					<c:if test="${p.hospital_state }">
					
					</c:if>
				</div>
			</div>
		</c:forEach>
		</div>
		<!-- 患者卡片  End -->
		
		<!-- 患者弹出层  Start -->
		<div class="easyui-window person-nurseWin" title="医嘱明细" style="width:965px;height:500px;user-select:none;"
			data-options="modal:true,collapsible:false,minimizable:false,
			maximizable:false,closed:true,draggable:false,shadow:true,resizable:false" style="padding:20px 40px">
			<div class="easyui-tabs" id="nuserTabs" fit="true">
				<div title="医嘱执行" id="record_Nurse">
					 <div style="padding: 5px;background: #dee;">
						<div style="display: inline-block;margin: 0 10px;color: gray;">
							<span style="display: inline-block;">
								住院号：<input name="patient_hid" class="text-css" disabled="disabled" style="color:black;width: 80px;padding: 0 15px;">
							</span>
							<span style="display: inline-block;margin-left: 20px;">
								床位：<input name="bedText" class="text-css" disabled="disabled" style="color:black;width: 30px;padding: 0 15px;">
							</span>
							<span style="display: inline-block;margin-left: 20px;">
								姓名：<input name="nameText" class="text-css" disabled="disabled" style="color:black;width: 70px;padding: 0 15px;">
							</span>
							<span style="display: inline-block;margin-left: 20px;">
								性别：<input name="sexText" class="text-css" disabled="disabled" style="color:black;width: 30px;padding: 0 15px;">
							</span>
							<span style="display: inline-block;margin-left: 20px;">
								年龄：<input name="ageText" class="text-css" style="width: 30px;padding: 0 15px;color:black;" disabled="disabled">
							</span>
							<span id="nurseTip" style="display: inline-block;margin-left: 20px;">
								押金余额：<input name="balanceText" class="text-css" style="width: 70px;padding: 0 15px;color:black;" disabled="disabled">
							</span>
						</div>
					 </div>
					 <table fitColumns="true" id="oneTable" class="easyui-datagrid" style="height:350px">
					
					 </table>
			
					 <div style="padding: 10px;">
					 	<div style="background:#3FBA93;display: inline-block;width:12px;height: 10px">
					 	
					 	</div>
					 	可执行
					 </div>
				</div>
				<div title="执行记录">
					<table id="excuteTable" class="easyui-datagrid" style="height:360px;">   
				
					</table>
				</div>
			</div>
		</div>
		<!-- 患者弹出层  End -->
	</div>
	<!-- 患者列表  End -->
	
	<div title="药品请领"> 
		<div style="width: fit-content;margin: 8px auto;">
			<fieldset style="float: left;border: 1px solid darkseagreen;border-radius: 5px;width: fit-content;">
				<legend>库存信息</legend>
				<p style="margin-top: 8px;">
					当前科室：
					<select id="deptBox" class="easyui-combobox" editable="false" panelHeight="auto"  style="width:120px;">   
						<option value="${user.departmentsPx.departmentsId }">${user.departmentsPx.departmentsName }</option>   
					</select>
					<span style="display: inline-block;margin-left: 20px">名称搜索：</span>
				 	<input id="deptDrugSear" class="easyui-searchbox" style="width:120px;height:28px;padding-left:5px;">
					<a id="appendDrug" class="easyui-linkbutton" style="padding: 0 5px;float: right;margin-right: 20px;" id="">添加</a>
				</p>
				<table id="deTable" class="easyui-datagrid" fitColumns=true style="width:600px;height:360px">
					<thead>
						
					</thead>
				</table>
			</fieldset>
			
			<fieldset style="border: 1px solid darkseagreen;border-radius: 5px;width: fit-content;">
				<legend>药品请领</legend>
				<p style="margin-top: 8px;">
					<span style="display: inline-block;border: 1px solid darkseagreen;border-radius: 3px;height: 24px;padding: 0 15px;padding-top: 3px;">
						默认请领数量：
						<label style="display: inline-block;width: 42px;">
							<input value="1" checked="checked" name="amount" type="radio" style="float: left;margin-top: 3px;cursor: pointer;">50
						</label>
						<label style="display: inline-block;width: fit-content;">
							<input value="2" name="amount" type="radio" style="float: left;margin-top: 3px;cursor: pointer;">100
						</label>
					</span>
					<a id="getBtn" class="easyui-linkbutton" style="padding: 0 5px;float: right;margin-right: 10px;" id="">确认请领</a>
					<a id="clearBtn" class="easyui-linkbutton" style="padding: 0 5px;float: right;margin-right: 10px;" id="">清除</a>
				</p>
				<table id="getTable" class="easyui-datagrid" fitColumns=true style="width:400px;height:360px">
					<thead>
						
					</thead>
				</table>
			</fieldset>
		</div>
	</div>
	 
	<div title="请领记录"> 
		<fieldset id="getRecord" style="margin: 0 auto;;border: 1px solid darkseagreen;border-radius: 5px;width: fit-content;">
			<legend>请领记录</legend>
			<p style="margin-top: 8px;">
				
				<span style="display: inline-block;margin-left: 20px">科室搜索：</span>
			 	<input id="invoiceRecordSear" class="easyui-searchbox" style="width:120px;height:28px;padding-left:5px;">
			</p>
			<table id="invoiceRecordTable" class="easyui-datagrid" fitColumns=true style="width:900px;height:360px">
				<thead>
					
				</thead>
			</table>
		</fieldset>
		<!-- 请领详情弹框 -->
		<div class="easyui-window detailsWin" title="请领详情" style="width:550px;height:420px;user-select:none;"
				data-options="modal:true,collapsible:false,minimizable:false,
				maximizable:false,closed:true,draggable:false,shadow:true,resizable:false" style="padding:20px 40px">
		 	<table id="detailsTable" class="easyui-datagrid" style="height:380px;">   
	
			</table>
		</div>
		
	</div>	
	
</div>
</body>
</html>